import React, { Component } from 'react'

export default class Demo1 extends Component {

  state={count:0}

  add=()=>{
    /* const { count } = this.state
    //对象式的setState
    this.setState({count:count+1},()=>{
      console.log(this.state.count)
    }) */

    //函数式的setState,传入一个返回更新完的state对象，默认接收state、props
    this.setState(state=>({count:state.count+1}))
      
  }

  render() {
    const { count } = this.state
    return (
      <div>
        <h2>当前state为：{count}</h2>
        <button onClick={this.add}>+1</button>
      </div>
    )
  }
}
